/*============================================================================
 * Project    : GameZone
 * Name       : NormalContainer.cpp
 * Author     : Administrator
 * Version    : 1.0
 * Copyright  : Copyright (c) 2009,Aspire-tech technology com, Ltd.
 * All rights reserved.
 * Created on : 2010-10-2 ����11:39:28
 * Description: {description}
 *============================================================================
 */

#include "NormalContainer.h"

const TInt KCbaHeight = 51;//28

CNormalContainer::CNormalContainer()
{
    iContainer = NULL;
    iTitleBar = NULL;
    iCbaBar = NULL;
    iInfoBar = NULL;
}

CNormalContainer::~CNormalContainer()
{
    delete iContainer;
    iContainer = NULL;
    delete iTitleBar;
    iTitleBar = NULL;
    delete iCbaBar;
    iCbaBar = NULL;
    delete iInfoBar;
    iInfoBar = NULL;
}

CNormalContainer* CNormalContainer::NewL( 
        const TRect& aRect, 
        const CCoeControl* aParent, 
        MEikCommandObserver* aCommandObserver )
    {
    CNormalContainer* self = CNormalContainer::NewLC( 
            aRect, 
            aParent, 
            aCommandObserver );
    CleanupStack::Pop( self );
    return self;
    }

CNormalContainer* CNormalContainer::NewLC( 
        const TRect& aRect, 
        const CCoeControl* aParent, 
        MEikCommandObserver* aCommandObserver )
    {
    CNormalContainer* self = new ( ELeave ) CNormalContainer();
    CleanupStack::PushL( self );
    self->ConstructL( aRect, aParent, aCommandObserver );
    return self;
    }
            
void CNormalContainer::ConstructL( 
        const TRect& aRect, 
        const CCoeControl* aParent, 
        MEikCommandObserver* aCommandObserver )
    {
    if ( aParent == NULL )
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL( *aParent );
        }
    iFocusControl = NULL;
    iCommandObserver = aCommandObserver;
    InitializeControlsL(aRect);
    SetRect( aRect );
    ActivateL();
    }

TInt CNormalContainer::CountComponentControls() const
    {
    return ( int ) ELastControl;
    }
                
CCoeControl* CNormalContainer::ComponentControl( TInt aIndex ) const
    {
    switch ( aIndex )
        {
        case ETitleBar:
            return iTitleBar;
        case EInfoBar:
            return iInfoBar;
        case EContainer:
            return iContainer;
        case ECbaBar:
            return iCbaBar;
        }
    return NULL;
    }
                            
void CNormalContainer::SizeChanged()
    {
    CCoeControl::SizeChanged();
    LayoutControls();
    }

void CNormalContainer::LayoutControls()
{
    TPoint pt;
    TInt yOffset = 0;
    TSize sz;
    if (iTitleBar)
    {
        sz = iTitleBar->Rect().Size();
        pt.iX = 0;
        pt.iY = 0;
        iTitleBar->SetExtent(pt, sz);
        yOffset += sz.iHeight;
    }
    
    if (iInfoBar)
    {
        sz = iInfoBar->Rect().Size();
        pt.iX = 0;
        pt.iY = yOffset;
        iInfoBar->SetExtent(pt, sz);
        yOffset += sz.iHeight;
    }
    sz = TSize(Rect().Width(), Rect().Height() - yOffset - KCbaHeight);
    iContainer->SetExtent(TPoint(0, yOffset), sz);
    if (iCbaBar)
    {
        TSize sz(iCbaBar->Rect().Width(), KCbaHeight);
        pt.iX = (Rect().Width() - iCbaBar->Rect().Width()) / 2;
        pt.iY = Rect().iBr.iY - KCbaHeight;
        iCbaBar->SetExtent(pt, sz);
    }
}
         
TKeyResponse CNormalContainer::OfferKeyEventL( 
        const TKeyEvent& aKeyEvent, 
        TEventCode aType )
    {
    if ( iFocusControl != NULL
        && iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
        {
        return EKeyWasConsumed;
        }
    return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
    }
                        
void CNormalContainer::InitializeControlsL(const TRect& aRect)
    {
    //����
    iTitleBar = CGuiControlFactory::CreateTitleBar();
    TInt yOffset = iTitleBar->Rect().Height();
    //info
    iInfoBar = CGuiControlFactory::CreateInfoBar();
    yOffset += iInfoBar->Rect().Height();
    //�б�
    TSize sz;
    sz = TSize(aRect.Width(),aRect.Height() - yOffset - KCbaHeight);
    TRect rect;
    rect.SetRect(TPoint(0, yOffset), sz);
    iContainer = CGuiControlFactory::CreateContainer(rect,this);
    
    //cba
    iCbaBar = CGuiControlFactory::CreateCbaBar(aRect,this,_L("\u6253\u5F00"),_L("\u9000\u51FA"));//��  �˳�
    setCbaL();
    
    iContainer->SetFocus( ETrue );
    iFocusControl = iContainer;
    }

void CNormalContainer::HandleResourceChange( TInt aType )
    {
    CCoeControl::HandleResourceChange( aType );
    SetExtentToWholeScreen();
    }

void CNormalContainer::setCbaButtonTextL(const TDesC& aLeft, const TDesC& aRight)
{
    if (iCbaBar)
    {
        iCbaBar->SetLeftTextL(aLeft);
        iCbaBar->SetRightTextL(aRight);
    }
}

void CNormalContainer::setTitleTextL(const TDesC& aText)
{
    if (iTitleBar)
    {
        iTitleBar->setTitleTextL(aText);
    }
}

void CNormalContainer::setCbaL()
{
    
}

void CNormalContainer::HandleControlEventL( CCoeControl* /*aControl*/, TEventType /*aEventType*/)
{

}

void CNormalContainer::AddControlL(CCoeControl* aControl)
{
    iContainer->AddControlL(aControl);
}

void CNormalContainer::UpdateControls(TInt aOffset)
{
    iContainer->UpdateControls(aOffset);
}

void CNormalContainer::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
	if(iInfoBar)
	  {
		  TRect infoBarRect=iInfoBar->Rect();
		  if(infoBarRect.Contains(aPointerEvent.iPosition))
		  {
			  return ;
		  }
	  }
    if (iContainer)
		{
		TRect Rect = iContainer->Rect();
		if (Rect.Contains(aPointerEvent.iPosition))
			{
			iContainer->SetFocus(ETrue);
			iContainer->HandlePointerEventL(aPointerEvent);
			DrawNow();
			return;
			}
		}
}
